﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using FunctionalCLR;
using FunctionalCLR.Core;

namespace Zove.Compiler.EquationTransformer {
    public class RouteFinder {
        private string wanted = string.Empty;

        public RouteFinder(string wanted) {
            this.wanted = wanted;
            this.Route = new Stack<int>();
        }

        public Stack<int> Route {
            get;
            set;
        }

        public bool Visit(Node node) {
            if (node.GetType().IsSubclassOf(typeof(OperatorNode))) {
                if (Visit(((OperatorNode)node).Left)) {
                    this.Route.Push(0);
                    return true;
                } else if (Visit(((OperatorNode)node).Right)) {
                    this.Route.Push(1);
                    return true;
                }
            } else if (node.GetType().IsSubclassOf(typeof(FuncNode))) {
                FuncNode fn = (FuncNode)node;
                for (int index = 0; index < fn.Parameters.Count; index++) {
                    if (Visit(fn.Parameters[index])) {
                        this.Route.Push(index);
                        return true;
                    }
                }
            } else if (node.GetType().Equals(typeof(IdentifierNode))
                || node.GetType().IsSubclassOf(typeof(IdentifierNode))
                ) {
                if (node.Value == wanted) {
                    return true;
                }
            }

            return false;
        }
    }
}
